package com.opal.system.controller;

import com.opal.consts.CommonConst;
import com.opal.domain.ReturnMessageData;
import com.opal.system.entity.table.OpalMenu;
import com.opal.system.service.OpalMenuService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/manage/menu")
public class MenuController {
	@Autowired
	private OpalMenuService opalMenuService;

	/**
	 * 根据session用户id获取相应的菜单并按easyui相应的规则封装数据；形成树
	 * @return
	 */
	@RequestMapping("/opalmenu")
	@ResponseBody
	public ReturnMessageData getOpalMenu() {
		ReturnMessageData	opalMenus=opalMenuService.getListOpalMenu();

		return opalMenus;
	}

	/**
	 * 根据用户id获取用户菜单：当前数据用于选中tree中的节点
	 * @return
	 */
	@RequestMapping("/getOpalMenuByUserId")
	@ResponseBody
	public List<OpalMenu> getMenuIdByUserId(Long userId) {
		List<OpalMenu> menuIdByRoleIdService = opalMenuService.getOpalMenuByUserIdService(userId);
		return menuIdByRoleIdService;
	}

	/**
	 * 修改菜单权限
	 * @param userId
	 * @param menuIds
	 * @return
	 */
	@RequestMapping("/updateMenuPermission")
	@ResponseBody
	public ReturnMessageData updateMenuPermission(Long userId, @RequestParam("menuIds[]") Long[] menuIds) {
		ReturnMessageData	opalMenus=opalMenuService.updateMenuPermissionService(userId,menuIds);

		return opalMenus;
	}

	/**
	 * 菜单管理页面的datagrid数据
	 * @return
	 */
	@RequestMapping("/MenuAllData")
	@ResponseBody
	public List<OpalMenu> getMenuAllData(){
		List<OpalMenu> menuAll= opalMenuService.getMenuAllDataservice();
		return menuAll;
	}

	@RequestMapping("/getAllMenu")
	@ResponseBody
	public List<OpalMenu> getAllMenu(){
		List<OpalMenu> menuAll= opalMenuService.getAllMenu();
		return menuAll;
	}

	/**
	 * 添加菜单
	 * @param opalMenu
	 * @return
	 */
	@RequestMapping("/addmenu")
	@ResponseBody
	public ReturnMessageData addMenu(OpalMenu opalMenu) {
		ReturnMessageData rmd=	opalMenuService.addMenuService(opalMenu);
		return rmd;
	}
	
	/**
	 * 修改菜单
	 * @param opalMenu
	 * @return
	 */
	@RequestMapping("/updatemenu")
	@ResponseBody
	public ReturnMessageData updateMenu(OpalMenu opalMenu) {
		ReturnMessageData rmd=	opalMenuService.updateMenuService(opalMenu);
		return rmd;
	}
	
	/**
	 * 删除菜单
	 * @return
	 */
	@RequestMapping("/deletemenu")
	@ResponseBody
	public ReturnMessageData deleteMenu(@RequestBody OpalMenu[] opalMenu) {
		ReturnMessageData resultMessage= opalMenuService.opalMenuSercice(opalMenu);
		return resultMessage;
	}

	/**
	 * 权限管理页面：权限路径字段的下拉树列表需要用到
	 * @return
	 */
	@RequestMapping("/permissionAllMenu")
	@ResponseBody
	public Object  permissionAllMenu(){
		Object list= opalMenuService.permissionAllMenuService();

		return list;
	}

	@RequiresRoles(CommonConst.ADMIN)
	@RequestMapping("{page}")
	public String skipPage(@PathVariable("page") String page) {
		return "permission_setting/"+page;
	}
}
